package cre.tomcat4.ch03.connector;

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * tomcat connector
 *
 * @Author penglai
 * @Date 2017/1/4.
 */
public class HttpConnector implements Runnable {

    private boolean stopped;
    private String scheme = "http";

    public void run() {
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(8080, 100, InetAddress.getByName("127.0.0.1"));
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }

        while (!stopped) {
            Socket socket = null;
            try {
                socket = serverSocket.accept();

                HttpProcessor processor = new HttpProcessor();
                processor.process(socket);
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
                continue;
            }
        }
    }

    public void start() {
        Thread thread = new Thread(this);
        thread.start();
    }
}
